﻿// Copyright (C) 2009 - 2013 EvTech Project
//
// This program is free software: you can redistribute it and/or modify
// it under the +terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Piirto
{
    public partial class TextInputDialog : Form
    {
        public string WrittenText;

        public TextInputDialog(String captionText, String labelText, String okButtonText, String cancelButtonText)
        {
            InitializeComponent();
            btnOk.Enabled = false;
            this.Text = captionText;
            this.label1.Text = labelText;
            this.btnOk.Text = okButtonText;
            if (cancelButtonText == null || cancelButtonText.Length == 0)
            {
                this.btnCancel.Visible = false;
            }
            else
            {
                this.btnCancel.Visible = true;
                this.btnCancel.Text = cancelButtonText;
            }
            resizeText(inputLbl);
            resizeText(label1);
            resizeText(btnOk);
            resizeText(btnCancel);
        }

        private void inputLbl_TextChanged(object sender, EventArgs e)
        {
            if (inputLbl.Text.Length > 0)
            {
                btnOk.Enabled = true;
                resizeText(inputLbl);
            }
            else
            {
                btnOk.Enabled = false;
                inputLbl.Refresh();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            WrittenText = inputLbl.Text;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (inputLbl.Text.Length > 0)
            {
                inputLbl.Text = inputLbl.Text.Remove(inputLbl.Text.Length - 1);
            }
        }

        private void buttonChar_Click(object sender, EventArgs e)
        {
            inputLbl.Text += ((Button)sender).Text;
        }

        private void resizeText(Control ctrl)
        {
            if (ctrl == null)
            {
                return;
            }
            float fontSize = ctrl.Font.Size;
            SizeF textSize;
            if (ctrl is Label)
            {
                ((Label)ctrl).UseCompatibleTextRendering = true;
            }
            Graphics gr = ctrl.CreateGraphics();
            textSize = gr.MeasureString(ctrl.Text, ctrl.Font);
            if (textSize.Width == 0)
            {
                return;
            }
            fontSize = fontSize * ctrl.ClientRectangle.Width / textSize.Width;
            ctrl.Font = new Font(ctrl.Font.FontFamily, fontSize);

            textSize = gr.MeasureString(ctrl.Text, ctrl.Font);
            if (textSize.Height > ctrl.ClientRectangle.Height)
            {
                fontSize = fontSize * ctrl.ClientRectangle.Height / textSize.Height;
                ctrl.Font = new Font(ctrl.Font.FontFamily, fontSize);
            }
            gr.Dispose();
            ctrl.Refresh();
        }

        private void TextInputDialog_Resize(object sender, EventArgs e)
        {
            resizeText(inputLbl);
            resizeText(label1);
            resizeText(btnOk);
            resizeText(btnCancel);
        }

        private void TextInputDialog_Shown(object sender, EventArgs e)
        {
            resizeText(inputLbl);
            resizeText(label1);
            resizeText(btnOk);
            resizeText(btnCancel);
        }

        private void btn_space_Click(object sender, EventArgs e)
        {
            inputLbl.Text += " ";
        }
    }
}
